import {createRouter,createWebHashHistory} from 'vue-router'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import {getToken} from '../utils/token'

const asyncRoutes=[
    {path:'/',redirect:'/home'},
    {
        path:'/home',
        component:()=>import('../views/home/Home.vue'),
        meta:{
            isShow:true,
            login:false,
            title:'首页'
        }
    },
    {
        path:'/article',
        component:()=>import('../views/article/Article.vue'),
        meta:{
            isShow:true,
            login:true,
            title:'种草文章'
        }
    },
    {
        path:'/cart',
        component:()=>import('../views/cart/Cart.vue'),
        meta:{
            isShow:true,
            title:'购物车'
        }
    },
    {
        path:'/user',
        component:()=>import('../views/user/User.vue'),
        meta:{
            isShow:true,
            login:true,
            title:'我的信息'
        }
    },
    {
        path:'/login',
        component:()=>import('../views/login/Login.vue'),
        meta:{
            isShow:false,
            title:'登录'
        }
    },
    {
        path:'/goods/:goodsId',
        component:()=>import('../views/home/GoodsDetail.vue'),
        meta:{
            isShow:false,
            title:'商品详情'
        }
    },
    {
        path:'/address',
        component:()=>import('../views/address/Address.vue'),
       meta:{
        login:true,
        title:'地址列表'
       }
    },
   {
    path:'/edit-address',
    component:()=>import('../views/address/EditAddress.vue'),
    meta:{
        login:true,
        title:'编辑地址'
    }
   },
   {
    path:'/order',
    component:()=>import('../views/order/AddOrder.vue'),
    meta:{
        login:true,
        title:'创建订单'
    }
   },
   {
    path:'/my-order',
    component:()=>import('../views/order/MyOrder.vue'),
    meta:{
        login:true,
        title:'我的订单'
    }
   },
   //文章详情
   {
    path:'/article-detail/:articleId',
    component:()=>import('../views/article/ArticleDetail.vue'),
    meta:{
        login:true,
        title:'文章详情'
    }
   },
   {
    path:'/add-article',
    component:()=>import('../views/article/AddArticle.vue'),
    meta:{
        login:true,
        title:'添加文章'
    }
   },
   {
    path:'/user-info',
    component:()=>import('../views/user/UserInfo.vue'),
    meta:{
        login:true,
        title:'我的信息'
    }
   },
   {
    path:'/edit-password',
    component:()=>import('../views/user/EditPassword.vue'),
    meta:{
        login:true,
        title:'修改密码'
    }
   },
   {
    path:'/my-article',
    component:()=>import('../views/article/MyArticle.vue'),
    meta:{
        login:true,
        title:'我的文章'
    }
   },
   {
    path:'/my-collection',
    component:()=>import('../views/user/MyCollection.vue'),
    meta:{
        login:true,
        title:'我的收藏'
    }
   },
   {
    path:'/my-praise',
    component:()=>import('../views/user/MyPraise.vue'),
    meta:{
        login:true,
        title:'我的点赞'
    }
   },
]

let router=createRouter({
    history:createWebHashHistory(),
    routes:[
        ...asyncRoutes,
        {path:'/:pathMatch(.*)',redirect:'/'}
    ]
})

Nprogress.configure({showSpinner:false})

router.beforeEach((to,from,next)=>{
    Nprogress.start()
    if(getToken()){
        if(to.path=='/login'){
            return next({
                path:from.path
            })
        }
    }else{
        if(to.meta.login){
            return  next('/login')
        }
    }
    next()
})

router.afterEach((to,from,next)=>{
    Nprogress.done()
    if(to.meta.title){
        document.title='码路严选--'+to.meta.title
    }
})

export default router